<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>绑定手机-{eyou:global name='web_name' /}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    {eyou:static file="users/skin/css/basic.css" /}
    {eyou:static file="users/skin/css/eyoucms.css" /}
    <!-- 新样式 2020-11-25 -->
    {eyou:static file="users/skin/css/element/index.css" /}
    {eyou:static file="users/skin/css/e-user.css" /}
    {eyou:include file="users/skin/css/diy_css.htm" /}
    
    {eyou:static file="/public/static/common/js/jquery.min.js"/}
    {eyou:static file="/public/plugins/layer-v3.1.0/layer.js"/}
    {eyou:static file="/public/static/common/js/tag_global.js"/}

</head>
<body>
<div class="el-row">
    <form name='theForm' id="theForm" method="post">
        <div class="ey-popup">
            <div class="el-form-item">
                <div class="el-input">
                    <input type="text" name="mobile" id="mobile" {eyou:eq name='$users.is_mobile' value='0'} value="{$users.mobile}" {/eyou:eq} required class="el-input__inner" autocomplete="off" placeholder="新的手机号码" maxlength="11">
                    
                </div>
            </div>
            <div class="el-form-item">
                <div class="el-input el-input-group el-input-group--append el-input-group--prepend reg">
                    <input type="text" name="vertify" id="vertify" autocomplete="off" class="el-input__inner" placeholder="图片验证码" />
                    <img src="{eyou:url link='api/Ajax/vertify' vars='type=bind' /}" class="chicuele" id="imgVerifys" onClick="fleshVerify();">
                </div>
            </div>
            <div class="el-form-item">
                <div class="el-input el-input-group el-input-group--append el-input-group--prepend">
                     <input type="text" class="el-input__inner" id="mobile_code" name="mobile_code" autocomplete="off" placeholder="手机验证码">
                     <div class="el-input-group__append">
                          <input type="button" id="mobile_button" onclick="get_mobile_code();" class="el-button el-button--default" value="获取验证码" />
                     </div>  
                </div>
            </div>
            <div class="el-form-item">
                <button type="button" class="el-button el-button--primary" style="width: 100%;" onclick="SubmitData();">确定</button>
            </div>
        </div>
        
        <!-- <div class="modal-body">
            <div class="form-group">
                <div class="input-group">
                    <input type="text" name="mobile" id="mobile" {eyou:eq name='$users.is_mobile' value='0'} value="{$users.mobile}" {/eyou:eq} required class="form-control" autocomplete="off" placeholder="新的手机号码" maxlength="11">
                </div>
            </div>

            <div class="form-group">
                <div class="input-group yxrz">
                    <input type="text" class="form-control" id="mobile_code" name="mobile_code" autocomplete="off" placeholder="手机验证码">
                    <input type="button" id="mobile_button" onclick="get_mobile_code();" class="btn btn-primary" value="获取验证码" />
                </div>
            </div>

        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-primary" style="width: 100%;" onclick="SubmitData();">确定</button>
        </div> -->
    </form>
</div>

<script type="text/javascript">
    function fleshVerify(){
        var src = "{eyou:url link='api/Ajax/vertify' vars='type=bind' /}";
        if (src.indexOf('?') > -1) {
            src += '&';
        } else {
            src += '?';
        }
        src += 'r='+ Math.floor(Math.random()*100);
        $('#imgVerifys').attr('src', src);
    }

    function get_mobile_code() {
        var mobile = $("#mobile").val();
        // 手机号是否为空
        if (!mobile) {
            $("#mobile").focus();
            layer.msg('请输入新的手机号码！', {time: 1500});
            return false;
        }
        
        // 手机格式不正确
        var reg = /^1[0-9]{10}$/i;
        if (!reg.test(mobile)) {
            $("#mobile").focus();
            layer.msg('请输入正确的新的手机号码！', {time: 1500});
            return false;
        }

        // 图形验证码
        var vertify = $("#vertify").val();
        if (!vertify) {
            $("#vertify").focus();
            layer.msg('请输入图形验证码！', {time: 1500});
            return false;
        }

        // 设置为不可点击
        $("#mobile_button").val('获取中…').attr('disabled', 'disabled');
        layer_loading('正在处理');
        $.ajax({
            url: '{$RootDir}/index.php?m=api&c=Ajax&a=SendMobileCode&_ajax=1',
            data: {mobile: mobile, is_mobile: true, source: 1, vertify:vertify, type:'bind'},
            type:'post',
            dataType:'json',
            success:function(res){
                layer.closeAll();
                if (res.code == 1) {
                    countdown();
                    layer.msg(res.msg, {time: 1500});
                } else {
                    $("#mobile_button").val('获取验证码').removeAttr("disabled");
                    showErrorAlert(res.msg);
                }
            },
            error : function(e) {
                layer.closeAll();
                $("#mobile_button").val('获取验证码').removeAttr("disabled");
                showErrorAlert(e.responseText);
            }
        });
    }

    function countdown() {
        // 倒计时
        var setTime;
        var time = 120;
        setTime = setInterval(function() {
            if(0 >= time){
                clearInterval(setTime);
                return;
            }
            time--;
            $("#mobile_button").val(time+'秒').attr('disabled', 'disabled');
            if(time == 0) $("#mobile_button").val('获取验证码').removeAttr("disabled");
        }, 1000);
    };

    function SubmitData() {
        var mobile = $("#mobile").val();
        var reg = /^1[0-9]{10}$/i;
        // 邮箱格式不正确
        if(!reg.test(mobile)) {
            layer.msg('请正确输入手机号码！', {time: 2000});
            return false;
        }

        var mobile_code = $("#mobile_code").val();
        if(!mobile_code) {
            layer.msg('手机验证码不能为空！', {time: 2000});
            return false;
        }

        var _parent = parent;
        var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        layer_loading('正在处理');
        $.ajax({
            url: '{$RootDir}/index.php?m=user&c=Users&a=bind_mobile&_ajax=1',
            data: $('#theForm').serialize(),
            type:'post',
            dataType:'json',
            success:function(res){
                layer.closeAll();
                if (res.code == 1) {
                    parent.layer.close(parentObj);
                    parent.layer.msg(res.msg, {time: 1500}, function() {
                        _parent.window.location.reload();
                    });
                } else {
                    showErrorAlert(res.msg);
                }
            },
            error : function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    };
</script>

</body>
</html>